package ecweb.ui.gooditem;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ecweb.domain.AdminUser;
import ecweb.domain.GoodItem;
import ecweb.domain.User;
import ecweb.exception.DatabaseException;
import ecweb.service.ICartService;
import ecweb.service.IGoodService;
import ecweb.service.impl.CartService;
import ecweb.service.impl.GoodService;

/**
 * Servlet implementation class ShowGoodItemServlet
 */
@WebServlet("/GoodItem/ShowGoodItem")
public class ShowGoodItemServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	private IGoodService goodService = new GoodService();
	private ICartService cartService = new CartService();
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowGoodItemServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String strID = request.getParameter("id");
		
		long id = 0;
		try {
			id = Long.parseLong(strID);
		} catch (NumberFormatException e) {
			//Do nothing
		}
		
		GoodItem gItem = null;
		try {
			gItem = goodService.getGoodById(id);
		} catch (DatabaseException e) {
			response.sendError(500);
			return;
		}
		
		if (gItem == null ) {
			response.sendError(404);
			return;
		}
		
		request.setAttribute("item", gItem);
		request.getRequestDispatcher("show_gooditem.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		if ("addToCart".equals(action)) {
			doAddToCart(request, response);
		} else if ("deleteGoodItem".equals(action)) {
			doDeleteGoodItem(request, response);
		} else {
			response.sendError(403);
		}
	}

	private void doAddToCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		
		User user = (User) session.getAttribute("user");
		if (user == null) {
			response.sendRedirect(request.getContextPath() + "/Account/Login");
			return;
		}
		
		String strBuyCount = request.getParameter("data");
		int buyCount = 0;
		try {
			buyCount = Integer.parseInt(strBuyCount);
		} catch (NumberFormatException e) {
			//Do nothing
		}
		if (buyCount <= 0)
			buyCount = 1;
		
		String strID = request.getParameter("id");
		long id = 0;
		try {
			id = Long.parseLong(strID);
		} catch (NumberFormatException e) {
			//Do nothing
		}
		
		GoodItem gItem = null;
		try {
			gItem = goodService.getGoodById(id);
		} catch (DatabaseException e) {
			response.sendError(500);
			return;
		}
		
		if (gItem == null ) {
			response.sendError(404);
			return;
		}
		
		try {
			int currentCount = cartService.getQuantity(user.getId(), id);
			int newCount = currentCount + buyCount;
			if (newCount < 0)
				newCount = 0;
			else if (newCount > gItem.getInventory())
				newCount = gItem.getInventory();
			cartService.setCartItem(user.getId(), id, newCount);
		} catch (DatabaseException e) {
			response.sendError(500);
			return;
		}

		response.sendRedirect(request.getContextPath() + "/Cart/ShowMyCart");
	}
	
	private void doDeleteGoodItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		
		AdminUser admin = (AdminUser) session.getAttribute("admin");
		if (admin == null) {
			response.sendError(403);
			return;
		}
		
		String strID = request.getParameter("id");
		long id = 0;
		try {
			id = Long.parseLong(strID);
		} catch (NumberFormatException e) {
			//Do nothing
		}
		
		GoodItem gItem = null;
		try {
			gItem = goodService.getGoodById(id);
		} catch (DatabaseException e) {
			response.sendError(500);
			return;
		}
		
		if (gItem == null ) {
			response.sendError(404);
			return;
		}
		
		try {
			goodService.deleteGood(id);
		} catch (DatabaseException e) {
			response.sendError(500);
			return;
		}
		
		request.getRequestDispatcher("delete_gooditem_success.jsp").forward(request, response);
	}
}
